package com.jhf.skypac_pda.adapter;

import android.content.Context;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.guanaj.easyswipemenulibrary.EasySwipeMenuLayout;
import com.jhf.skypac_pda.FinishedProductStockInActivity;
import com.jhf.skypac_pda.R;
import com.jhf.skypac_pda.db.bean.DB_OperationRecord;
import com.jhf.skypac_pda.db.gen.DB_OperationRecordDao;
import com.jhf.skypac_pda.db.impl.OperationRecordDaoImp;
import com.jhf.skypac_pda.model.FinishedProductModel;
import com.jhf.skypac_pda.model.OrderDetailModel;

import java.util.List;

public class NewOrderDetailListAdapter extends BaseQuickAdapter<DB_OperationRecord, BaseViewHolder> {
    public NewOrderDetailListAdapter(int layoutResId, @Nullable List<DB_OperationRecord> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, DB_OperationRecord item) {
        helper.setText(R.id.material_code, item.getMaterialCode());
        helper.setText(R.id.tv_stock_location_code, item.getStockLocationCode());
        helper.setText(R.id.tv_scan_material_code_time, item.getScanPalletCodeTime());
        helper.setText(R.id.tv_stock_location_code_time, item.getScanLocationCodeTime());

        String status = item.getMaterialTankFeedingRecordStatusDesc();
        helper.setText(R.id.order_status, status);
        if (status.equals("未开始")) {
            helper.setTextColor(R.id.order_status, mContext.getResources().getColor(R.color.title_text_num));
        } else if (status.contains("完成")) {
            helper.setTextColor(R.id.order_status, mContext.getResources().getColor(R.color.status_feeding_done));
        } else if (status.equals("投料中") || status.equals("进行中")) {
            helper.setTextColor(R.id.order_status, mContext.getResources().getColor(R.color.status_feeding));
        } else {
            helper.setTextColor(R.id.order_status, mContext.getResources().getColor(R.color.title_text_quit));
        }

        helper.getView(R.id.left).setVisibility(View.GONE);
        if (item.getMaterialTankFeedingRecordStatusDesc().equals("未开始")) {
            helper.getView(R.id.right_menu_2).setVisibility(View.VISIBLE);
            helper.getView(R.id.right_menu_2).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(mContext, "删除成功", Toast.LENGTH_SHORT).show();
                    //deleteItem(item);
                    // 从数据源中删除数据
                    int position = getData().indexOf(item);
                    getData().remove(item);
                    notifyItemRemoved(position); //

                    //数据库同步删除
                    OperationRecordDaoImp.getInstance(mContext).deleteRecordById(item.getId());

                    EasySwipeMenuLayout easySwipeMenuLayout = helper.getView(R.id.es);
                    easySwipeMenuLayout.resetStatus();

                    if (mViewClickListener != null) {
                        mViewClickListener.post_refresh();
                    }
                }
            });
        } else {
            helper.getView(R.id.right_menu_2).setVisibility(View.GONE);
        }
    }

    public interface ViewClickListener{
        void post_refresh();
    }

    public void setViewClickListener(NewOrderDetailListAdapter.ViewClickListener mViewClickListener){
        this.mViewClickListener=mViewClickListener;

    }

    private NewOrderDetailListAdapter.ViewClickListener mViewClickListener;

}
